import urllib.request
import urllib.parse
import json
from lxml import etree
import sys
import os
import numpy as np

if __name__ == '__main__':
    pageUrl = "https://datachart.500.com/ssq/history/newinc/history.php?start={}&end={}"
    result_file = "ssq-history.csv"
    bottom = '03001'

    ori_data = np.loadtxt(result_file, delimiter=",", dtype=np.str)

    if (ori_data.__len__() > 0):
        cur = str(int(max(ori_data[..., 0])) + 1).zfill(5)
    else:
        cur = bottom

    end = str(int(cur[0:2]) + 1).zfill(2) + '001'

    pageUrl = pageUrl.format(cur, end)
    print(pageUrl)

    # headers = {
    #     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
    #     "Accept-Encoding": "gzip, deflate, br",
    #     "Accept-Language": "zh-CN,zh;q=0.9",
    #     "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
    # }

    request = urllib.request.Request(url=pageUrl, method="GET")

    html = urllib.request.urlopen(request).read().decode('utf-8', errors='ignore')

    result = etree.HTML(html)

    ## 期数
    list1 = result.xpath("//tr[@class='t_tr1']/td[1]/text()")

    ## 蓝色球
    list2 = result.xpath("//tr[@class='t_tr1']/td[@class='t_cfont2'][1]/text()")
    list3 = result.xpath("//tr[@class='t_tr1']/td[@class='t_cfont2'][2]/text()")
    list4 = result.xpath("//tr[@class='t_tr1']/td[@class='t_cfont2'][3]/text()")
    list5 = result.xpath("//tr[@class='t_tr1']/td[@class='t_cfont2'][4]/text()")
    list6 = result.xpath("//tr[@class='t_tr1']/td[@class='t_cfont2'][5]/text()")
    list7 = result.xpath("//tr[@class='t_tr1']/td[@class='t_cfont2'][6]/text()")

    ## 红球
    list8 = result.xpath("//tr[@class='t_tr1']/td[@class='t_cfont4'][1]/text()")

    ## 开奖日期
    list9 = result.xpath("//tr[@class='t_tr1']/td[last()]/text()")

    # npList = np.array(list1,list2,list3,list4,list5,list6,list7,list8,list9)
    npList = np.array([list1, list2, list3, list4, list5, list6, list7, list8, list9])

    new_data = npList.transpose()

    if new_data.__len__() > 0:
        with open(result_file, "r+") as f:
            old = f.read()
            f.seek(0, 0)
            np.savetxt(f, new_data, fmt="%s", delimiter=",")
            f.write(old)

    cur_date = np.loadtxt(result_file, delimiter=",", dtype=np.str)

    blue_data = np.array(cur_date[..., 1:7]).reshape(1, -1)
    red_data = np.array(cur_date[..., 7]).reshape(1, -1)

    blue_map = {}
    red_map = {}

    for x in range(1, 34):
        num = str(x).zfill(2)
        blue_map[num] = np.sum(blue_data == num)

    for x in range(1, 17):
        num = str(x).zfill(2)
        red_map[num] = np.sum(red_data == num)

    blue_result = sorted(blue_map.items(), key=lambda item: item[1])
    print(list(map(lambda item: item[0],blue_result[0:7])))

    red_result = sorted(red_map.items(), key=lambda item: item[1])
    print(list(map(lambda item: item[0],red_result[0:2])))
